/*
 * FileUtils.java
 *
 * Created on 06-Aug-2007, 11:29:58
 *
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.accason.litecommons.util.io;

import java.io.File;

/**
 *
 * @author accason
 */
public class FileUtils {
  
  public FileUtils() {
  }
  
  /**
   * <p>Remove directory (or file) and its contents recursively.</p>
   * <p>Useage:</p>
   * <blockquote><pre>
   * File testDirectory = new File("testDirectory");
   * testDirectory.mkdir();
   * FileUtils fileUtils = new FileUtils();
   * boolean isRemoved = fileUtils.recursiveRemove(testDirectory);
   * </pre></blockquote>
   * @param filePath The diretory or file that want to remove.
   * @return true - Removed successfully, false - Remove unsuccessfully.
   */
  public boolean recursiveRemove(File filePath) {
    // Remove all sub directories and files.
    if (filePath.exists() && filePath.isDirectory()) {
      File[] files = filePath.listFiles();
      for (File file : files) {
        if (file.isDirectory()) {
          recursiveRemove(file);
        } else {
          file.delete();
        }
      }
    }
    
    // Remove root directory and return.
    return filePath.delete();
  }
  
  /**
   * Remove directories (or files) and its contents recursively.
   * @todo coding...
   */
  public boolean recursiveRemove(File[] filePaths) {
    boolean flag = true;
    for (File file : filePaths) {
      boolean isRemoved = this.recursiveRemove(file);
      if (!isRemoved) {
        flag = false;
      }
    }
    
    return flag;
  }
  
  /**
   * Empty directory contents recursively. Return false if gived is not directory.
   * @param directory
   * @return true - Emp
   * @todo doing...
   */
  public boolean emptyDirectory(File directory) {
    if (directory.isDirectory()) {
      File[] files = directory.listFiles();
      return this.recursiveRemove(files);
    } else {
      return false;
    }
  }
}